﻿using System;
using System.Buffers;
using System.Collections.Generic;

namespace _20_Strategy
{
    /// <summary>
    /// 策略模式
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //策略是一种行为设计模式， 它将一组行为转换为对象， 并使其在原始上下文对象内部能够相互替换。
            //var context = new Context();
            //Console.WriteLine("Client:Strategy is set to normal sorting");
            //context.SetStrategy(new ConcreteStrategyA());
            //context.DoSomeBusinessLogic();

            //Console.WriteLine();

            //Console.WriteLine("Client:Strategy is set to reverse sorting");
            //context.SetStrategy(new ConcreteStrategyB());
            //context.DoSomeBusinessLogic();


            //var students = new Student().AddStudent();
            //var result = new Demo().GetScoreName(students);
            //foreach (var student in result) {
            //    Console.WriteLine(student.Key+":"+student.Value);
            //}

            var students = new Student().AddStudent();
            var result = new Dictionary<int, string>();
            foreach (var student in students)
            {
                Context context01 = new Context(new StrategyImp01());
                var result01 = context01.ExecuteStrategy(student.Score);
                if (result01!="")
                {
                    result.Add(student.Id,result01);
                }
                Context context02 = new Context(new StrategyImp02());
                var result02 = context02.ExecuteStrategy(student.Score);
                if (result02!="")
                {
                    result.Add(student.Id, result02);
                }
                Context context03 = new Context(new StrategyImp03());
                var result03 = context03.ExecuteStrategy(student.Score);
                if (result03!="")
                {
                    result.Add(student.Id, result03);
                }
                Context context04 = new Context(new StrategyImp04());
                var result04 = context04.ExecuteStrategy(student.Score);
                if (result04!="")
                {
                    result.Add(student.Id, result04);
                }
            }
            foreach (var item in result)
            {
                Console.WriteLine(item.Key+":"+item.Value);
            }


            Console.ReadKey();
        }
    }
    //class Context
    //{
    //    private IStrategy _strategy;
    //    public Context()
    //    {

    //    }
    //    public Context(IStrategy strategy)
    //    {
    //        _strategy = strategy;
    //    }
    //    public void SetStrategy(IStrategy strategy)
    //    {
    //        _strategy = strategy;
    //    }
    //    public void DoSomeBusinessLogic()
    //    {
    //        Console.WriteLine("Context: Sorting data using the strategy (not sure how it'll do it)");
    //        var result = _strategy.DoAlgorithm(new List<string> { "a", "b", "c", "d", "e" });
    //        string resultStr = string.Empty;
    //        foreach (var element in result as List<string>)
    //        {
    //            resultStr += element + ",";
    //        }
    //        Console.WriteLine(resultStr);
    //    }
    //}
    //public interface IStrategy
    //{
    //    object DoAlgorithm(object data);
    //}
    //class ConcreteStrategyA : IStrategy
    //{
    //    public object DoAlgorithm(object data)
    //    {
    //        var list = data as List<string>;
    //        list.Sort();
    //        return list;
    //    }
    //}
    //class ConcreteStrategyB : IStrategy
    //{
    //    public object DoAlgorithm(object data)
    //    {
    //        var list = data as List<string>;
    //        list.Sort();
    //        list.Reverse();
    //        return list;
    //    }
    //}
}
